package bar.dark.lee.common;

import net.daum.adam.publisher.AdView;
import net.daum.adam.publisher.AdView.AnimationType;
import net.daum.adam.publisher.AdView.OnAdFailedListener;
import net.daum.adam.publisher.AdView.OnAdLoadedListener;
import net.daum.adam.publisher.AdView.OnAdWillLoadListener;
import net.daum.adam.publisher.impl.AdError;
import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.RelativeLayout;
import bar.dark.lee.BDConstents;

import com.cauly.android.ad.AdInfo;
import com.cauly.android.ad.AdListener;

public class BDAd
{
	public final static int MAXCOUNT = 5;
	private int requestCount = 0;
	
	private Context context = null;
	private LinearLayout linearLayout = null;
	private FrameLayout frameLayout = null;
	
	/** 카울리 광고 정보  */
	private AdInfo adinfo;
	/** 카울리 광고 뷰 */
	private com.cauly.android.ad.AdView caulyView = null;
	
	/** 아담 광고 뷰 */
	private AdView adamView = null;
	
//	private int adHeight;
	
	public BDAd(Context context, LinearLayout adviewLinear) {
		this.context = context;
		this.linearLayout = adviewLinear;
		this.linearLayout.setVisibility(View.VISIBLE);
		
		this.frameLayout = new FrameLayout(context);
		
//		// 광고 높이를 48dip로 설정
//		adHeight = (int) TypedValue.applyDimension(
//				TypedValue.COMPLEX_UNIT_DIP, (float) 48.0, context.getResources().getDisplayMetrics());
		
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		this.frameLayout.setLayoutParams(params);
		this.linearLayout.addView(frameLayout);
		
		//광고 정보 셋팅
        adinfo = new AdInfo();
        adinfo.setPriority(Thread.NORM_PRIORITY); //Thread 우선 순위 (1 ~ 10) 
        adinfo.initData(BDConstents.CAULY_APP_KEY, "cpm", "all", "all", "off", "default", "yes", 0, false);
	}
	
	public void startAd()
	{
		startAdam();
	}

	private void startAdam()
	{
		// Ad@m 광고 뷰 생성 및 설정
		adamView = new AdView(context);
		
		adamOnClickListener();
		
		// 할당 받은 clientId 설정
		adamView.setClientId(BDConstents.ADAM_APP_KEY);
		
        adamView.setRequestInterval(12);
        adamView.setGravity(Gravity.BOTTOM);
 		adamView.setAnimationType(AnimationType.NONE);
 		adamView.setVisibility(View.VISIBLE);
 		
 		frameLayout.addView(adamView);
	}
	
	private void startCauly()
	{
		adinfo = new AdInfo();
        adinfo.setPriority(Thread.NORM_PRIORITY); //Thread 우선 순위 (1 ~ 10) 
        adinfo.initData(BDConstents.CAULY_APP_KEY, 
        		"cpc", "all", "all", "off", "left_slide", "yes", 30, true);
        
        //광고 View
        caulyView = new com.cauly.android.ad.AdView(context);
		caulyView.setVisibility(View.VISIBLE);
		
		caulyOnClickListener();
		
		frameLayout.addView(caulyView);
	}
	
	public void resumeAd()
	{
		requestCount = 0;
		
		destoryAd();
		
		startAd();
	}
	
	public void pauseAd()
	{
//		if (adamView != null)
//		{
//			adamView.pause();
//		}
//		
//		if (caulyView != null)
//		{
//			caulyView.stopLoading();
//		}
		
		destoryAd();
	}
	
	public void destoryAd()
	{
		if (adamView != null)
		{
			adamView.destroy();
			adamView = null;
		}
		
		if (caulyView != null)
		{
			caulyView.destroy();
			caulyView = null;
		}
	}
	
	public void adamOnClickListener()
	{
		if (adamView != null)
		{
			adamView.setOnAdLoadedListener(new OnAdLoadedListener()
			{
				@Override
				public void OnAdLoaded()
				{
					requestCount = 0;
					
					if (caulyView != null)
					{
						caulyView.destroy();
						caulyView = null;
					}
				}
			});
			
			adamView.setOnAdFailedListener(new OnAdFailedListener()
			{
				@Override
				public void OnAdFailed(AdError arg0, String arg1)
				{
					if (adamView != null)
					{
						if (MAXCOUNT >= requestCount)
						{
							requestCount++;
							destoryAd();
							startAd();
						}
						else
						{
							requestCount = 0;
							startCauly();
						}
					}
				}
			});
		}
	}
	
	public void caulyOnClickListener()
	{
		if (caulyView != null)
		{
			caulyView.setAdListener(new AdListener()
			{
				@Override
				public void onReceiveAd()
				{
					requestCount = 0;
					
					if (adamView != null)
					{
						adamView.destroy();
						adamView = null;
					}
				}
				
				@Override
				public void onFailedToReceiveAd(boolean arg0)
				{
					if (caulyView != null)
					{
						if (MAXCOUNT >= requestCount)
						{
							requestCount++;
							
							destoryAd();
							
							startCauly();
						}
					}
				}
				
				@Override
				public void onCloseInterstitialAd()
				{
				}
			});
		}
	}
}
